<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="提现类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择提现类型" clearable>
          <el-option
              v-for="dict in withdrawal_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:textractaudit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="textractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="提现人" align="center" prop="userName"/>
      <el-table-column label="提现人类型" align="center" prop="userType">
        <template #default="scope">
          <dict-tag :options="t_extract_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="amount"/>
      <el-table-column label="提交时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="提现类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="withdrawal_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" width="250" align="center" prop="bankcardNo"/>
      <el-table-column label="手机号" width="150" align="center" prop="bankcardPhone"/>
      <el-table-column label="支付宝号" width="150" align="center" prop="alipayAccount"/>
      <el-table-column label="状态" align="center" prop="status" fixed="right">
        <template #default="scope">
          <dict-tag :options="t_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Checked" @click="handleAudit(scope.row)"
                     v-hasPermi="['system:textractaudit:audit']">审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 审核 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="textractRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核意见" prop="auditStatus">
              <el-radio-group v-model="form.auditStatus">
                <el-radio
                    v-for="dict in t_audit"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核原因" prop="statusReason">
              <el-input v-model="form.statusReason" placeholder="请输入审核原因"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="auditSubmitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Textract">
import {
  listTextract,
  getTextract,
  updateStatus
} from "@/api/system/textract";

const {proxy} = getCurrentInstance();
const {
  withdrawal_type,
  t_status,
  t_audit,
  t_extract_user_type
} = proxy.useDict('withdrawal_type', 't_status', 't_audit', 't_extract_user_type');

const textractList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mnyId: undefined,
    amount: undefined,
    type: undefined,
    alipayAccount: undefined,
    paymentCode: undefined,
    bankcardNo: undefined,
    bankcardPhone: undefined,
    status: '01',
    statusReason: undefined,
  },
  rules: {
    auditStatus: [
      {required: true, message: "审核意见不能为空", trigger: "blur"}
    ],
    statusReason: [
      {required: true, message: "审核原因不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询提现列表列表 */
function getList() {
  loading.value = true;
  listTextract(queryParams.value).then(response => {
    textractList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    extractId: undefined,
    mnyId: undefined,
    amount: undefined,
    type: undefined,
    alipayAccount: undefined,
    paymentCode: undefined,
    bankcardNo: undefined,
    bankcardPhone: undefined,
    status: '01',
    statusReason: undefined,
    auditTime: undefined,
    auditor: undefined,
    ext1: undefined,
    ext2: undefined,
    ext3: undefined,
    state: undefined,
    createTime: undefined,
    creator: undefined,
    modifyTime: undefined,
    modifier: undefined
  };
  proxy.resetForm("textractRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.extractId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/textract/export', {
    ...queryParams.value
  }, `textract_${new Date().getTime()}.xlsx`)
}

/** 审核操作 */
function handleAudit(row) {
  reset();
  form.value = row;
  form.value.statusReason = ''
  open.value = true;
  title.value = "审核提现";
}

function auditSubmitForm(row) {
  proxy.$refs["textractRef"].validate(valid => {
    if (valid) {
      if (form.value.extractId != null) {
        form.value.status = form.value.auditStatus;
        updateStatus(form.value).then(response => {
          proxy.$modal.msgSuccess("审核成功");
          open.value = false;
          getList();
        });
      } else {

      }
    }
  });
}

onActivated(() => {
    getList();
})

getList();
</script>
